using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FarPoint.Win.Spread;

using Spread.Common.DataStore;
using Spread.Common.DataStore.Models;

namespace GrapeCity.AgingReport
{
    public partial class AgingReport : Spread.Common.Features.Sample.BaseSampleControl
    {
        #region Fields
        FpSpread fpSpread;
        private DateTime _agingDate;
        private JsonDataSource _dataSource;
        private List<Customer> _customers;
        private List<Invoice> _invoices;
        #endregion

        #region ctor
        public AgingReport()
        {
            InitializeComponent();
            SetupSpread();
            this.Load += AgingReport_Load;
        }
        #endregion
        #region Event Handlers
        private void AgingReport_Load(object sender, EventArgs e)
        {
            _agingDate = DateTime.Now.Date;
            _dataSource = new DataService().JsonDataSource;
            _customers = _dataSource.GetAgingReportCustomers();
            var random = new Random();
            var date = new DateTime(2016, random.Next(8, 12), random.Next(1, 28));
            _invoices = _dataSource.GetAgingReportInvoices(date, date.AddYears(1)).Take(100).ToList();
            _invoices.ForEach(invoice => invoice.CreatedOn = invoice.CreatedOn.AddYears(5));
            fpSpread.OpenExcel(@"Template\AgingReport.xlsx");
            LoadInvoices(fpSpread.Sheets["Invoices"]);
            SetCustomerNames(fpSpread.Sheets["Aging Report"], 8, 2);

            fpSpread.Sheets[0].Cells[4, 8].Value = new DateTime(2022, 11, 15);
            fpSpread.Sheets[0].RowCount = 50;
            fpSpread.Sheets[1].RowCount = 110;
            fpSpread.Sheets[0].ColumnCount = 15;
            fpSpread.Sheets[1].ColumnCount = 15;
            fpSpread.Sheets[0].Cells[7, 2, 7, 8].Locked = false;
        }
        #endregion
        #region Private methods
        private void LoadInvoices(SheetView sheet)
        {
            int row = 1, col = 0;
            var query = _invoices.Join(_customers, inv => inv.CustomerID, cust => cust.ID, (inv, cust) =>
               new
               {
                   InvoiceID = inv.ID,
                   CustomerName = cust.Name,
                   InvoiceDate = inv.CreatedOn,
                   InvoiceAmount = inv.TotalAmount
               });

            foreach (var data in query)
            {
                sheet.Cells[row, col].Text = data.CustomerName;
                sheet.Cells[row, col + 1].Value = data.InvoiceID;
                sheet.Cells[row, col + 2].Value = data.InvoiceDate;
                sheet.Cells[row, col + 4].Value = data.InvoiceAmount;
                ++row;
            }

            // Set Aging Date
            sheet.Cells[0, 12].Value = DateTime.Now.Date;
            sheet.Columns[11].Width = sheet.Columns[11].GetPreferredWidth();
            sheet.Columns[12].Width = sheet.Columns[12].GetPreferredWidth();
        }

        private void SetCustomerNames(SheetView sheet, int row, int col)
        {
            foreach (Customer customer in _customers)
            {
                sheet.Cells[row, col].Text = customer.Name;
                ++row;
            }
        }
        private void SetupSpread()
        {
            fpSpread = new FpSpread(LegacyBehaviors.None) { Dock = DockStyle.Fill };
            this.pnlMain.Controls.Add(fpSpread);
        }
        #endregion        
    }
}

//
// AgingReport.Designer.cs
//

namespace GrapeCity.AgingReport
{
    partial class AgingReport
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Size = new System.Drawing.Size(878, 593);
            // 
            // pnlControls
            // 
            this.pnlControls.Size = new System.Drawing.Size(218, 593);
            // 
            // AgingReport
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "AgingReport";
            this.Size = new System.Drawing.Size(878, 593);
            this.ResumeLayout(false);

        }

        #endregion
    }
}